package org.grow.jssp.service;

import org.grow.jssp.entity.JsspFile;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:宿世新
 * @Dte:2022/3/14
 */
@Service
public class FileOps implements FileOperator {

    @Override
    public List<JsspFile> ops(String fileName) {
        File file = new File(fileName);
        File[] oldList = file.listFiles();
        //过滤掉文件夹
//        Map<String, Integer> newList = new HashMap<>();
        List<File> newList = new ArrayList<>();
        for (int i = 0; i < oldList.length; i++) {
            File item = oldList[i];
            if (item.isFile()) {
                String t = fileName + File.separator + item.getName();
                Long length = new File(t).length();
                newList.add(item);
            }
        }
        //映射f(x)
        List<JsspFile> finalList = new ArrayList<>();
        for (int i = 0; i < newList.size(); i++) {
            File item = newList.get(i);
            JsspFile jsspFile = new JsspFile();
            jsspFile.setTitle(item.getName());
            jsspFile.setSize((double) item.length() / 1024);
            finalList.add(jsspFile);
        }
        System.out.println(newList);
        return finalList;
    }
}


